﻿using System;
using System.IO;
using System.Windows;
using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using MyFloridaHouse.libSvKMZ.KML;

namespace MyFloridaHouse.libSvKMZ
{
    public class KMZ
    {


        //CreateKmzFileToDisk("C:\\Data\\KML\\Plans\\", "test", kmlstream, dojstream);
        public static void CreateKmzStream(string zipFileName, MemoryStream kmzFileStream, MemoryStream kmlFileStream, MemoryStream dojFileStream)
        {

            using (ZipOutputStream zipStream = new ZipOutputStream(kmzFileStream))
            {
                zipStream.IsStreamOwner = false;	// False stops the Close also Closing the underlying stream.
                zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
                zipStream.UseZip64 = UseZip64.Off;

                // create KML entry
                zipStream.PutNextEntry(CreateZipEntry(zipFileName + ".kml"));
                kmlFileStream.Seek(0, 0);
                StreamUtils.Copy(kmlFileStream, zipStream, new byte[4096]);

                // create DOJ entry
                zipStream.PutNextEntry(CreateZipEntry(zipFileName + ".doj"));
                dojFileStream.Seek(0, 0);
                StreamUtils.Copy(dojFileStream, zipStream, new byte[4096]);

                zipStream.CloseEntry();
                zipStream.Close();			// Must finish the ZipOutputStream before using outputMemStream.
            }

        }


        public static ZipEntry CreateZipEntry(string zipEntryName)
        {
            MemoryStream outputMemStream = new MemoryStream();
            ZipEntry newEntry = new ZipEntry(zipEntryName);
            newEntry.DateTime = DateTime.Now;


            return newEntry;
        }

        public static bool ReadKmzStream(Stream KMZFileStream, out MemoryStream kmlFileStream, out MemoryStream dojFileStream)
        {
            ZipInputStream zipStream = new ZipInputStream(KMZFileStream);
            kmlFileStream = new MemoryStream();
            dojFileStream = new MemoryStream();

            if (zipStream == null)
            {
                MessageBox.Show("Sorry, zip file is empty ");
                return false;
            }
            else
            {
                bool foundKML = false;
                bool foundDOJ = false;

                ZipEntry theEntry;
                string tmpEntry = String.Empty;
                while ((theEntry = zipStream.GetNextEntry()) != null)
                {
                    string fileName = theEntry.Name;

                    if (fileName != String.Empty)
                    {
                        if (theEntry.Name.Contains(".kml"))
                        {
                            StreamUtils.Copy(zipStream, kmlFileStream,new byte[4096]);
                            foundKML = true;
                        }
                        if (theEntry.Name.Contains(".doj"))
                        {
                            StreamUtils.Copy(zipStream, dojFileStream, new byte[4096]);
                            foundDOJ = true;
                        }
                    }
                }
                zipStream.Close();

                if (foundKML & foundDOJ)
                {

                    kmlFileStream.Seek(0, new SeekOrigin());
                    dojFileStream.Seek(0, new SeekOrigin());
                    return true;
                }
                else
                {
                    return false;
                }


            }
        }


        public static kml ExtractKML(Stream fileDataStream, ref MemoryStream DOJstream, ref MemoryStream KMLstream)
        {
            kml newKml = null;
            if (KMZ.ReadKmzStream(fileDataStream, out KMLstream, out DOJstream))
            {
                newKml = kml.Deserialize(KMLstream);
                if (newKml == null)
                {
                    throw new Exception("Unable to extract info from KML file");
                }
                else if (newKml.Items == null)
                {
                    throw new Exception("Unable to extract any items from KML file");
                }

            }
            return newKml;
        }

    }
}
